package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import com.itheima.service.CheckGroupService;
import com.itheima.service.SetmealService;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.utils.QiniuUtils;
import com.mysql.jdbc.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Reference
    public SetmealService setmealService;

    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = setmealService.findPage(queryPageBean);
        return pageResult;
    }

    @GetMapping("/findById")
    public Result findById(Integer id){
        Setmeal setmeal = setmealService.findById(id);
        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
    }
    @GetMapping("/delete")
    public Result delById(Integer id) {
        try {
            Integer result = setmealService.delById(id);
            if(result==1 ){
                return new Result(true,"删除成功");
            }else if(result==-1){
                return new Result(false,"检查组被套餐绑定，无法删除！");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false,"删除失败");
    }
    @PostMapping("findCheckgroupIdsWithSetmeal")
    public Result findCheckgroupIdsWithSetmeal(Integer id) {
        List<Integer> list = setmealService.findCheckgroupIdsWithSetmeal(id);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, list);
    }



    @PostMapping("/edit")
    public Result update(@RequestBody Setmeal setmeal, Integer[] checkgroupId){
        Integer result = setmealService.update(setmeal,checkgroupId);
        if (result == 1) {
            return new Result(true, "更新成功");
        }
        return new Result(false, "更新失败");
    }

    @GetMapping("/findCheckGroupIdsBySetmealId")
    public Result findCheckGroupIdsBySetmealId(Integer id){
        List<Integer> checkgroupIds = setmealService.findCheckGroupIdsBySetmealId(id);
        if (checkgroupIds.size() > 0){
            return new Result(true,null,checkgroupIds);
        }
        return new Result(false,"查询出错");
    }

    @GetMapping("/findAll")
    public Result findAll() {
        List<Setmeal> all = setmealService.findAll();
        if (all.isEmpty()) {
            return new Result(false, "查询出错");
        }
        return new Result(true, null, all);
    }
    @PostMapping("/add")
    public Result add(@RequestBody Setmeal setmeal , @RequestParam("checkgroupIds") List<Integer> checkGroupIds){
        if (setmeal.getCode() == null && setmeal.getName() == null) {
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        Integer count= this.setmealService.add(setmeal,checkGroupIds);
        if (count == 1) {
            return new Result(true,  MessageConstant.ADD_MEMBER_SUCCESS);
        }
        return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
    }

    @PostMapping("/upload")
    public Result upload(MultipartFile imgFile){
        try {
            if (imgFile == null){
                return new Result(false,"文件不存在");
            }
            String originalFilename = imgFile.getOriginalFilename();
            if (StringUtils.isNullOrEmpty(originalFilename)){
                return new Result(false,"文件不存在");
            }
            UUID uuid = UUID.randomUUID();
            String fileNameAnduuid = uuid+originalFilename;
            QiniuUtils.upload2Qiniu(imgFile.getBytes(), fileNameAnduuid);
            return new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,originalFilename);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
    }


}
